package com.wtp;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public abstract class CustomDialog extends Dialog {
	public Context context;
	Dialog dialog;

	public CustomDialog(Context context, int layout, int style) {
		super(context, style);
		setContentView(layout);
		this.context = context;
		dialog = this;
		Window window = getWindow();
		WindowManager.LayoutParams layoutParams = window.getAttributes();
		layoutParams.width = LayoutParams.MATCH_PARENT;
		layoutParams.height = LayoutParams.MATCH_PARENT;
		layoutParams.gravity = Gravity.CENTER;
		window.setAttributes(layoutParams);
		setCanceledOnTouchOutside(false);
		setCancelable(false);
//		setOnKeyListener(new OnKeyListener() {
//
//			@Override
//			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//				if (keyCode == KeyEvent.KEYCODE_BACK) {
//					return true;
//				} else {
//					return false; // 默认返回 false
//				}
//			}
//		});
		initListener();
		if (!(context instanceof Activity)) {
			getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		}
	}

	public abstract void initListener();

	@SuppressWarnings("unused")
	private float getDensity(Context context) {
		Resources resources = context.getResources();
		DisplayMetrics dm = resources.getDisplayMetrics();
		return dm.density;
	}

	public void CloseDialog() {
		dialog.dismiss();
	}

	@Override
	public void onBackPressed() {
		CloseDialog();
	}

}
